package com.darthsith.runtap.activity.fragment;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.darthsith.runtap.Constants;
import com.darthsith.runtap.R;
import com.darthsith.runtap.activity.MainActivity;

public class PreferenceFragment extends android.preference.PreferenceFragment implements OnClickListener {
	
	private TextView gameDuration;
	private Button back;
	private SharedPreferences preferences;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view= inflater.inflate(R.layout.preferences_fragment, container, false);
		back=(Button) view.findViewById(R.id.back);
		gameDuration=(TextView) view.findViewById(R.id.gameDuration);
		back.setOnClickListener(this);
		preferences=PreferenceManager.getDefaultSharedPreferences(getActivity());
		gameDuration.setText(Integer.valueOf(preferences.getInt(Constants.PREF_GAMEDURATION, 20)).toString());
		Log.i(getTag(), "Load PreferenceFragment");
		return view;
	}

	@Override
	public void onClick(View v) {
		if (v.getId()==back.getId()) {
			String prefDuration=gameDuration.getText().toString();
			if (prefDuration!="") {
				Log.i(getTag(), "Saving gameDuration: "+prefDuration);
				Editor editor=preferences.edit();
				editor.putInt(Constants.PREF_GAMEDURATION, Integer.parseInt(prefDuration));
				editor.commit();
			}
			((MainActivity)getActivity()).switchView(((MainActivity)getActivity()).getMenuFragment());
		}
	}

}
